#!/usr/bin/python
#example code, lecture 10

##divide conquer methods
def merge( left, right ):
    """assumes left and right are sorted lists, return a new sorted list containing the same elements as left+right. """
    result = [  ]
    i,j = 0,0
    while i < len( left ) and  j < len( right ):
        if left[ i ] <= right[ j ]:
            result.append( left[ i ] )
            i = i+1
        else:
            result.append( right[ j ] )
            j = j+1
    while ( i < len( left ) ):
        result.append( left[ i ] )
        i = i+1
    while ( j < len( right ) ):
        result.append( right[ j ] )
        j = j+1
    return result

def mergesort( L ):
    """return a new sort list containg the same elements as L"""
    print L
    if len( L )<2:
        return L[ : ]
    else:
        middle = len( L )/2
        left = mergesort( L[ :middle ] )
        right = mergesort( L[ middle: ] )
        together = merge( left,right )
        print 'merged', together
        return together

#test
#mergesort( [ 1,4,3,6,6,2,8,7 ] )

##Hashing
def create( smallest, largest ):
    intSet = [  ]
    for i in range( smallest, largest+1 ): inSet.append( None )
    return inSet

def insert( inSet, e ):
    inSet[ e ] = 1

def member( inSet, e ):
    #check membership
    return inSet[ e ] == 1

def hashChar( c ):
    # c is a char
    # function return a different integer in the range 0-255
    # for each possible value of c
    return ord( c )

def cSetCreate():
    cSet = [  ]
    for i in range( 0, 255 ): cSet.append( None )
    return cSet

def cSetInsert( cSet, e ):
    cSet[ hashChar[ e ] ] = 1

def cSetMember( cSet, e ):
    return cSet[ hashChar[ e ] ] == 1

##Exception
def readFloat( requestMsg, errorMsg ):
    while True:
        val = raw_input( requestMsg )
        try:
            val = float( val )
            return val
        except:
            print ( errorMsg )
            
#print readFloat('Enter float:', 'Not a float.')

def readVal( valType, requestMsg, errorMsg ):
    while True:
        val = raw_input( requestMsg )
        try:
            val = valType( val )
            return val
        except:
            print( errorMsg )

#print readVal(int, 'Enter int:', 'Not a int.')

def getGrades( fname ):
    try:
        gradesFile = open( fname, 'r' )
    except IOError:
        print 'Could not open', fname
        raise 'GetGradesError'
    grades = [  ]
    for line in gradesFile: grades.append( float( line ) )
    return grades

try:
    grades = getGrades('glgrades.txt')
    grades.sort()
    median = grades[ len( grades )/2 ]
    print 'Median grade is', median
except 'GetGradesError':
    print 'Whoops'
